package com.homepig.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;

/**
 *  @author fengchao
 */
public class DocumentStudy {
    public void insertDocumentOne(MongoCollection collection){
        Document canvas = new Document("item","canvas").append("qty",100).append("tags",singletonList("cotton"));
        Document size = new Document("h",28).append("w",35.5).append("l",50);
        canvas.put("size",size);
        collection.insertOne(canvas);
    }

    public void insertDocumentMany(MongoCollection collection){
        Document journal = new Document("item","journal").append("qry",250).append("tags",asList("blank","red"));
        Document mousepad = new Document("item","mousepad").append("qry",56).append("tags",asList("blue","wite"));
        collection.insertMany(asList(journal,mousepad));
    }

    public static void main(String[] args) {
        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase database = client.getDatabase("mongo_stu");
        MongoCollection collection = database.getCollection("test2");
        DocumentStudy study = new DocumentStudy();
        // study.insertDocumentOne(collection);
        study.insertDocumentMany(collection);
    }
}
